home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch14 / ZOrdGrid.cls < prev   
Text File  |  1999-06-22  |  4KB  |  138 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "ZOrderGrid3d"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private Xmin As Single      ' Min X and Y values.
  17. Private Zmin As Single
  18. Private Dx As Single        ' Spacing between rows of data.
  19. Private Dz As Single
  20. Private NumX As Integer     ' Number of X and Y entries.
  21. Private NumZ As Integer
  22. Private points() As Point3D ' Data values.
  23.  
  24. Public RemoveHidden As Boolean
  25.  
  26. Private Type POINTAPI
  27.     X As Long
  28.     Y As Long
  29. End Type
  30. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
  31.  
  32. ' Create the Points array.
  33. Public Sub SetBounds(ByVal x1 As Single, ByVal deltax As Single, ByVal xnum As Integer, ByVal z1 As Single, ByVal deltaz As Single, ByVal znum As Integer)
  34. Dim i As Integer
  35. Dim j As Integer
  36. Dim X As Single
  37. Dim Z As Single
  38.  
  39.     Xmin = x1
  40.     Zmin = z1
  41.     Dx = deltax
  42.     Dz = deltaz
  43.     NumX = xnum
  44.     NumZ = znum
  45.     ReDim points(1 To NumX, 1 To NumZ)
  46.     
  47.     X = Xmin
  48.     For i = 1 To NumX
  49.         Z = Zmin
  50.         For j = 1 To NumZ
  51.             points(i, j).coord(1) = X
  52.             points(i, j).coord(2) = 0
  53.             points(i, j).coord(3) = Z
  54.             points(i, j).coord(4) = 1#
  55.             Z = Z + Dz
  56.         Next j
  57.         X = X + Dx
  58.     Next i
  59. End Sub
  60. ' Save the indicated data value.
  61. Public Sub SetValue(ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
  62. Dim i As Integer
  63. Dim j As Integer
  64.  
  65.     i = (X - Xmin) / Dx + 1
  66.     j = (Z - Zmin) / Dz + 1
  67.     points(i, j).coord(2) = Y
  68. End Sub
  69.  
  70. ' Apply a transformation matrix which may not
  71. ' contain 0, 0, 0, 1 in the last column to the
  72. ' object.
  73. Public Sub ApplyFull(M() As Single)
  74. Dim i As Integer
  75. Dim j As Integer
  76.  
  77.     For i = 1 To NumX
  78.         For j = 1 To NumZ
  79.             m3ApplyFull points(i, j).coord, M, points(i, j).trans
  80.         Next j
  81.     Next i
  82. End Sub
  83.  
  84. ' Apply a transformation matrix to the object.
  85. Public Sub Apply(M() As Single)
  86. Dim i As Integer
  87. Dim j As Integer
  88.  
  89.     For i = 1 To NumX
  90.         For j = 1 To NumZ
  91.             m3Apply points(i, j).coord, M, points(i, j).trans
  92.         Next j
  93.     Next i
  94. End Sub
  95.  
  96.  
  97. ' Draw the transformed points on a PictureBox.
  98. Public Sub Draw(ByVal pic As Object)
  99. Dim i As Integer
  100. Dim j As Integer
  101. Dim api_points(1 To 4) As POINTAPI
  102.  
  103.     On Error Resume Next
  104.  
  105.     ' See if we should fill the "rectangles."
  106.     If RemoveHidden Then
  107.         pic.FillStyle = vbFSSolid
  108.         pic.FillColor = vbWhite
  109.     Else
  110.         pic.FillStyle = vbFSTransparent
  111.     End If
  112.  
  113.     ' Draw the "rectangles."
  114.     For i = 1 To NumX - 1
  115.         For j = 1 To NumZ - 1
  116.             ' Load the POINTAPI array.
  117.             With api_points(1)
  118.                 .X = points(i, j).trans(1)
  119.                 .Y = points(i, j).trans(2)
  120.             End With
  121.             With api_points(2)
  122.                 .X = points(i + 1, j).trans(1)
  123.                 .Y = points(i + 1, j).trans(2)
  124.             End With
  125.             With api_points(3)
  126.                 .X = points(i + 1, j + 1).trans(1)
  127.                 .Y = points(i + 1, j + 1).trans(2)
  128.             End With
  129.             With api_points(4)
  130.                 .X = points(i, j + 1).trans(1)
  131.                 .Y = points(i, j + 1).trans(2)
  132.             End With
  133.  
  134.             Polygon pic.hdc, api_points(1), 4
  135.         Next j
  136.     Next i
  137. End Sub
  138.